package net.htjs.juc.biased;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

/**
 * 测试偏向锁
 * 查看对象头
 * <!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
 * <dependency>
 *     <groupId>org.openjdk.jol</groupId>
 *     <artifactId>jol-core</artifactId>
 *     <version>0.10</version>
 * </dependency>
 */
@Slf4j
public class TestBiasedLock2 {

    static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            System.out.println(ClassLayout.parseInstance(object).toPrintable());
            synchronized (object) {
                System.out.println(ClassLayout.parseInstance(object).toPrintable());
            }
            System.out.println(ClassLayout.parseInstance(object).toPrintable());
            synchronized (TestBiasedLock2.class) {
                TestBiasedLock2.class.notify();
            }
        }, "t1").start();

        new Thread(() -> {
            synchronized (TestBiasedLock2.class) {
                try {
                    TestBiasedLock2.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            log.info(ClassLayout.parseInstance(object).toPrintable());
            synchronized (object) {
                log.info(ClassLayout.parseInstance(object).toPrintable());
            }
            log.info(ClassLayout.parseInstance(object).toPrintable());
        }, "t2").start();

    }
}
